package siteflash.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheManager;

import siteflash.data.SiteFlashPersistenceManage;
import siteflash.helper.Paginator;
import siteflash.model.Game;
import siteflash.model.Paginate;
import siteflash.model.Tag;

public class Popular extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) 
	    throws IOException {
	    req.setAttribute("title", "Y1S Game Online Free, Game Flash Free, Popular game");
	    req.setAttribute("keyword", "y1s, y1s.org, flash games, free flash games, online games, play game, fun, flash games online, popular game");
	    req.setAttribute("description", "Y1s.org Game Popular");
	    
	    PersistenceManager psm = SiteFlashPersistenceManage.get().getPersistenceManager();
	    //List all tags
	    ArrayList<Tag> tags = (ArrayList<Tag>) General.getTags();
	    int total = (int) General.getTotal(tags);
	    
	    //Set limit and number page
	    int limit = 18;
	    String strPage = (String) req.getParameter("page");
	    int page = 1;
	    if (strPage != null) {
	    	page = Integer.parseInt(strPage);
	    }
	    int start = (page - 1) * limit;
	    int end = page * limit;
	    
	    //List all game
	    ArrayList<Game> games = new ArrayList<Game>();
	    Cache cache=null;
		String cachename = "listgame_" + strPage;
		try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
           e.printStackTrace();
        }
        
        if (cache.containsKey(cachename)) {
        	games = (ArrayList<Game>) cache.get(cachename);
        	System.out.println("List Game: cache print");
        } else {
		    Query query = psm.newQuery(Game.class);
		    query.setOrdering("played desc");
		    query.setRange(start, end);
		    List<Game> listResult = (List<Game>) query.execute();
		    for (int i=0; i < listResult.size(); i++) {
		        games.add(listResult.get(i));
		    }
        }
        
	    //List hot game
	    ArrayList<Game> hotGames = (ArrayList<Game>) General.hotGames();
	    
	    //Paginate
	    Paginator paginator = new Paginator();
	    paginator.setLimit(limit);
	    Paginate paginate = paginator.paginate(page, total);
	    
	    req.setAttribute("pageName", "home");
	    req.setAttribute("tags", tags);
	    req.setAttribute("games", games);
	    req.setAttribute("hotGame", hotGames);
	    req.setAttribute("paginate", paginate);
	    try {
	        req.getRequestDispatcher("/pages/homepage.jsp").forward(req, resp);
	    } catch (Exception e) {
	        //TODO: handle exception
	        e.printStackTrace();
	    }
	}
}
